import os
import time
import psutil
from main import Document, SimilarityDetector, PlagiarismChecker

def test_function_performance(func, *args, **kwargs):
    process = psutil.Process(os.getpid())  # 获取当前进程
    start_time = time.time()  # 开始时间
    initial_memory = process.memory_info().rss  # 初始内存使用量

    result = func(*args, **kwargs)  # 执行函数

    end_time = time.time()  # 结束时间
    final_memory = process.memory_info().rss  # 最终内存使用量
    memory_used = final_memory - initial_memory  # 计算内存使用增量

    return {
        'result': result,
        'time': end_time - start_time,
        'memory': memory_used
    }

# 测试Document类的预处理函数
preprocess_result = test_function_performance(
    Document.preprocess,
    "This is a sample text for testing the preprocess function."
)
print(f"Preprocess Function - Time: {preprocess_result['time']}s, Memory: {preprocess_result['memory']} bytes")

# 测试SimilarityDetector类的compute_similarity函数
doc1 = Document("orig.txt")
doc2 = Document("orig_0.8_add.txt")
compute_similarity_result = test_function_performance(
    SimilarityDetector.compute_similarity,
    doc1,
    doc2
)
print(f"Compute Similarity Function - Time: {compute_similarity_result['time']}s, Memory: {compute_similarity_result['memory']} bytes")

# 测试SimilarityDetector类的lcs_length函数
lcs_length_result = test_function_performance(
    SimilarityDetector.lcs_length,
    doc1.tokens,
    doc2.tokens
)
print(f"LCS Length Function - Time: {lcs_length_result['time']}s, Memory: {lcs_length_result['memory']} bytes")

# 测试SimilarityDetector类的get_lcs函数
get_lcs_result = test_function_performance(
    SimilarityDetector.get_lcs,
    doc1.tokens,
    doc2.tokens
)
print(f"Get LCS Function - Time: {get_lcs_result['time']}s, Memory: {get_lcs_result['memory']} bytes")

# 测试PlagiarismChecker类的highlight_similarity函数
lcs = SimilarityDetector.get_lcs(doc1.tokens, doc2.tokens)
highlight_similarity_result = test_function_performance(
    PlagiarismChecker.highlight_similarity,
    doc1,
    doc2,
    lcs
)
print(f"Highlight Similarity Function - Time: {highlight_similarity_result['time']}s, Memory: {highlight_similarity_result['memory']} bytes")

# 测试PlagiarismChecker类的count_characters函数
count_characters_result = test_function_performance(
    PlagiarismChecker.count_characters,
    "path_to_document.txt"
)
print(f"Count Characters Function - Time: {count_characters_result['time']}s, Memory: {count_characters_result['memory']} bytes")

# 请确保替换"path_to_doc1.txt"、"path_to_doc2.txt"和"path_to_document.txt"为实际文件路径